package com.padakeji.android.tumou.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.padakeji.android.tumou.R;
import com.padakeji.android.tumou.TuMouActivity;
import com.padakeji.android.tumou.bean.News;
import com.padakeji.android.tumou.present.HomePresent;
import com.padakeji.android.tumou.util.TuMouUIHelper;

import java.util.List;

public class HomeActivity extends TuMouActivity implements HomeView {

    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private UiSettings uiSettings;

    private TextView latestOrHottestText, refreshOrCreateText, mineText;

    private HomePresent homePresent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mMapView = (MapView) findViewById(R.id.activity_map_map);
        mBaiduMap = mMapView.getMap();
        uiSettings = mBaiduMap.getUiSettings();
        setMapProperties();

        homePresent = new HomePresent(this);
        homePresent.locate();

        latestOrHottestText = (TextView) findViewById(R.id.activity_home_text_latestOrHottest);
        refreshOrCreateText = (TextView) findViewById(R.id.activity_home_text_refreshOrCreate);
        mineText = (TextView) findViewById(R.id.activity_home_text_mine);

        latestOrHottestText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        refreshOrCreateText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        refreshOrCreateText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                gotoPost();
                return true;
            }
        });

        mineText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    private void gotoPost() {
        Intent i = new Intent(this, PostActivity.class);
        startActivity(i);
    }

    private void setMapProperties() {
        mMapView.showZoomControls(false);
        mBaiduMap.setMyLocationEnabled(true);
        uiSettings.setRotateGesturesEnabled(false);
        uiSettings.setOverlookingGesturesEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_map, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void renderMyLocation(double lat, double lng) {
        LatLng ll = new LatLng(lat, lng);
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
        mBaiduMap.animateMapStatus(u);

        // 确定了位置之后，就开始去加载附近的新闻数据。
        homePresent.loadNewsData(lat, lng);
    }


    @Override
    public void renderNews(List<News> news) {
//        for (News n : news) {
//            View v = getLayoutInflater().inflate(R.layout.activity_home_news, mMapView, false);
//        }
        TuMouUIHelper.getInstance().showToast("" + news.toString(), Toast.LENGTH_SHORT);
    }

}
